package com.hk.aefz;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器
 */

public class MybatisPlusCode {

    public static final String driver = "com.mysql.jdbc.Driver";
    public static final String url = "jdbc:mysql://localhost:3306/wkblog?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
    public static final String username = "root";
    public static final String password = "123456";

    public static void main(String[] args) {
//      创建代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
//        全局配置
        GlobalConfig gc = new GlobalConfig();
//        获取系统当前位置
        String property = System.getProperty("user.dir");
        gc.setOutputDir(property+"/src/main/java");
        gc.setAuthor("阿尔法哲");
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setIdType(IdType.AUTO); //主键生成策略
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
//        数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName(driver);
        dsc.setUrl(url);
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setDbType(DbType.MYSQL); //数据库类型
        mpg.setDataSource(dsc);
//        包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("aefz");
        pc.setParent("com.hk");
        pc.setEntity("po");
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

//        策略配置
        StrategyConfig sc = new StrategyConfig();
        String[] s = new String[12];
        s[0] = "account";s[1] = "role";
        s[2] = "permissions";s[3] = "user_info";
        s[4] = "essay";s[5] = "comment";
        s[6] = "classify";s[7] = "praise";
        s[8] = "navigation";s[9] = "logs";
        s[10] = "label";s[11] = "special";
        sc.setInclude(s);//数组类型 映射的表
        sc.setNaming(NamingStrategy.underline_to_camel); // 表转java下划线转驼峰
        sc.setColumnNaming(NamingStrategy.underline_to_camel); // 未指定Naming
        sc.setEntityLombokModel(true);
        sc.setRestControllerStyle(true);
        sc.setControllerMappingHyphenStyle(true);
        TableFill gmt_create = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmt_modified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        List<TableFill> tableFillList = new ArrayList<>(2);
        tableFillList.add(gmt_create);
        tableFillList.add(gmt_modified);
        sc.setTableFillList(tableFillList);//自动填充字段
        mpg.setStrategy(sc);
//        模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);
        mpg.setTemplate(tc);
        mpg.execute(); //执行

    }

}
